package com.example.safeassistan.home.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.home.fragment.BackLogFragment;
import com.example.safeassistan.utile.EventBusBean;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class ActivityBacksecond  extends BaseActivity {

    private TabLayout tabLayout;
    private TextView tvSearch;
    private EditText etEdit;
    private ViewPager viewPager;
    private List<Fragment> fragmentList;
    private List<String> mTitleList; //Tab的标题
    private MainAdapter adapter;
    private String defId="";
    private String defRecordId="";//检查记录表id
    private String riskName="";//


    public static void actionStart(Context context,String defId,String defRecordId) {
        Intent intent = new Intent(context, ActivityBacksecond.class);
        intent.putExtra("defId",defId);//人物检查表
        intent.putExtra("defRecordId",defRecordId);//检查记录表
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.backlog_second;
    }

    @Override
    protected void initView() {
        setTitleText("设备维护人员常规现场检查");
        defId=getIntent().getStringExtra("defId");
        defRecordId=getIntent().getStringExtra("defRecordId");
        viewPager=findViewById(R.id.viewPager);
        tabLayout=findViewById(R.id.tabLayout);
        etEdit=findViewById(R.id.etEdit);
        tvSearch=findViewById(R.id.tvSearch);
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  搜索 发送广播
                EventBusBean eventBus=new EventBusBean();
                eventBus.setCode(105);
                eventBus.setMsg(riskName);
                EventBus.getDefault().post(eventBus);
            }
        });

        etEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                riskName=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        fragmentList=new ArrayList<>();
        fragmentList.add(BackLogFragment.newInstance(1,defId,defRecordId));
        fragmentList.add(BackLogFragment.newInstance(0,defId,defRecordId));
        mTitleList=new ArrayList<>();
        mTitleList.add("未检查");
        mTitleList.add("已检查");

        adapter=new MainAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) { }

            @Override
            public void onTabReselected(TabLayout.Tab tab) { }
        });
    }

    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }


    @Override
    protected void onResume() {
        super.onResume();
        EventBusBean eventBus=new EventBusBean();
        eventBus.setCode(105);
        eventBus.setMsg(riskName);
        EventBus.getDefault().post(eventBus);
    }

}